package client;

import java.awt.Color;
import java.awt.Graphics;

import com.jpemartin.jgt.Drawable;
import com.jpemartin.jgt.MoveControl;
import com.jpemartin.jgt.VisibleObject;

/** A generic "fire and forget" text animation **/
public class TextAnimation extends VisibleObject implements Drawable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 3536655935900370424L;
	/** the text we show */
    protected String _text;
    /** what moves us */
    protected MoveControl _move;
    /** time to live */
    protected int _ttl;
    /** number of turns the animation was present */
    protected int _lived;
    protected Color _color;
    
    /** Ctor. You can use the same object for several animations in a row by calling reset. */
    public TextAnimation(String text, int xStart, int yStart, Color color, MoveControl move, int timeToLive) {
        _text = text;
        x = xStart; y = yStart;
        _ttl = timeToLive;
        _move = move;
        _lived=0;
        _color = color;
    }

    /** puts the text animation at the given position with a given number of animation steps,
     * ready to be displayed.
     */
    public void reset(int xStart, int yStart, int timeToLive) {
       x = xStart;
       y = yStart;
       _alive = true;
       _lived=0;
       _ttl = timeToLive;
    }
    

    /** @return the color of the text */
   public Color getColor() {
      return _color;
   }

   /** Set the text color */
   public void setColor(Color _color) {
      this._color = _color;
   }
    
    /** update the position of the text (and _alive). Call this for every frame. */
    public void move() {
        _lived++;
        if (_lived>_ttl) _alive = false;
        _move.move(this);
    }

    /** draw the text. Call this for every frame. */
    public void draw(Graphics g) {
       if (!_alive || null==g) return;
       g.setColor(_color);
       g.drawString(_text,x,y); 
    }

    /** That's the "Drawable" version of Draw. Call this if you want to manage
     * the position of the text yourself instead of having the animation manage it
     * (the normal way to use TextAnimation).
     */
    public void draw(java.awt.Graphics g, int x, int y) {
       if (!_alive || null==g) return;
       g.setColor(_color);
       g.drawString(_text,x,y); 
    }
    
}
